package com.xiaoming.jdbc;

import java.sql.*;

public class JDBCTest {
    public static void main(String[] args) {
        PreparedStatement preparedStatement = null;
        Connection connection = null;

        try {
            // 加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 获取连接
            String url = "jdbc:mysql://118.25.134.32/gifts?characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false";
            connection = DriverManager.getConnection(url, "root", "123456");
            // 执行SQL获取执行结果
            String sql = "select * from user where id = ?";
            // 获取预处理statement
            preparedStatement = connection.prepareStatement(sql);
            // 设置参数
            preparedStatement.setString(1,"2");
            ResultSet resultSet = preparedStatement.executeQuery();
            // 解析返回值
            while(resultSet.next()){
                int age = resultSet.getInt("age");
                String name = resultSet.getString("name");
                System.out.println(age+" "+name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(connection!=null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
